#property copyright "Babs & Chris Corp."
#property link      "http://www.google.fr"

#include <stdlib.mqh>

#import "BlackCricket.dll"
   int eurusd(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, double a15, double a16, double a17);
   int gbpusd(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, double a15, double a16, double a17);
   int eurchf(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, double a15, double a16, double a17);
   int eurgbp(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, double a15, double a16, double a17);
   int usdjpy(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, double a15, double a16, double a17);
   int usdcad(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, double a15, double a16, double a17);
   int audusd(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, double a15, double a16, double a17);
   int eurjpy(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, double a15, double a16, double a17);
   int gbpjpy(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, double a15, double a16, double a17);
   int usdchf(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14, double a15, double a16, double a17);
#import

extern double riskpercent = 4.0;
extern double lots = 0.0;
extern double  MaxLot = 50;
extern int magicNumber = 1234;
extern int stoploss = 50;
extern int takeprofit = 100;
extern int Palier     = 100;
extern string rem4="//---- Extra";
/*extern int BreakEven=7;
extern int BreakoutPeriode=1;
extern int TrendPeriode=3;*/
extern int PartialTP1=30;
extern int PartialRatio1=50;
extern int PartialTP2=60;
extern int PartialRatio2=70;
extern bool allowTrending = TRUE;
extern int trendTrigger = 10;
extern int trendStoploss = 20;


double gd_112;
int Slippage = 3;
int g_bars_124 = 0;
int g_file_128 = -1;
bool gi_unused_132 = FALSE;
bool gi_unused_136 = FALSE;
bool gi_140 = FALSE;
bool gi_144 = FALSE;
double InitLot=0.1;
double InitBalance=0;
double MyPoint;
int FlagPartial=0;
bool Stealth_BUY=false;
bool Stealth_SELL=false;
double TrendStopLoss;

int init() {
   g_file_128 = FileOpen("BlackCricket.log", FILE_CSV|FILE_WRITE|FILE_READ, " ");
   gd_112 = riskpercent / 100.0;
   InitLot=lots;
   InitBalance=AccountBalance();
   if (Digits == 5 || Digits == 3) MyPoint = Point*10;
   else MyPoint= Point;
   if (Digits == 5 || Digits == 3) {
      takeprofit = 10 * takeprofit;
      stoploss = 10 * stoploss;
   } 
   TrendStopLoss=PartialTP1-30;
   gi_144 = true;
   return (0);
}

int deinit() {
   FileClose(g_file_128);
   if (!gi_144) {
      ObjectDelete("report");
      ObjectDelete("report2");
   }
   return (0);
}

//+------------------------------------------------------------------+
//| Money Management function                                        |
//+------------------------------------------------------------------+
void autoManagement(){
   int balance=0;
   balance=AccountBalance();
   if((balance-InitBalance)>=Palier){
      balance=(balance-InitBalance)/Palier;
      lots=InitLot+0.01*balance;
   }
   if(lots>=MaxLot)
      lots=MaxLot;
}

int start() {
   string ls_136;
   int li_144;
   if (isNewBar()) gi_140 = FALSE;
   if (gi_140) {
		OrdersList();
		managePositions();
   }
   autoManagement();
   
   if (allowTrending) {
      for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) {
         if (OrderSelect(l_pos_0, SELECT_BY_POS)) {
            if (magicNumber == OrderMagicNumber()) {
               if (OrderType() == OP_BUY)
                  if (OrderOpenPrice() + PartialTP1*Point - Bid <= trendTrigger * Point && Bid < OrderOpenPrice() + PartialTP1*Point) OrderModify(OrderTicket(), 0, Bid - trendStoploss * Point, 0, 0, White);
               if (OrderType() == OP_SELL)
                  if (Ask - (OrderTakeProfit()+ PartialTP1*Point) <= trendTrigger * Point && Ask > OrderOpenPrice() - PartialTP1*Point) OrderModify(OrderTicket(), 0, Ask + trendStoploss * Point, 0, 0, White);
            }
         }
      }
   }
   
   
   if(iCustom(NULL,Period(),"BlackCricketIndic",5,21,0,1)!=2147483647.00000000){
      Stealth_BUY=true;
      Stealth_SELL=false;     
   }
   if(iCustom(NULL,Period(),"BlackCricketIndic",5,21,1,1)!=2147483647.00000000){
      Stealth_SELL=true; 
      Stealth_BUY=false;
   }
   if (AccountFreeMargin() <= 0.0) {
      Print("There isn\'t enough money on your account to trade");
      FileWrite(g_file_128, "There isn\'t enough money on your account to trade");
      managePositions();
      return;
   }
   gi_unused_132 = FALSE;
   gi_unused_136 = FALSE;
   double l_irsi_0 = iRSI(NULL, 0, 8, PRICE_TYPICAL, 1);
   double l_irsi_8 = iRSI(NULL, 0, 16, PRICE_TYPICAL, 1);
   double l_irsi_16 = iRSI(NULL, 0, 24, PRICE_TYPICAL, 1);
   double l_icci_24 = iCCI(NULL, 0, 8, PRICE_TYPICAL, 1);
   double l_icci_32 = iCCI(NULL, 0, 16, PRICE_TYPICAL, 1);
   double l_icci_40 = iCCI(NULL, 0, 24, PRICE_TYPICAL, 1);
   double l_iwpr_48 = iWPR(NULL, 0, 8, 1);
   double l_iwpr_56 = iWPR(NULL, 0, 16, 1);
   double l_iwpr_64 = iWPR(NULL, 0, 24, 1);
   double l_istochastic_72 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, 1);
   double l_istochastic_80 = iStochastic(NULL, 0, 16, 3, 3, MODE_LWMA, 0, MODE_MAIN, 1);
   double l_istochastic_88 = iStochastic(NULL, 0, 24, 3, 3, MODE_LWMA, 0, MODE_MAIN, 1);
   double l_irvi_96 = iRVI(NULL, 0, 8, MODE_MAIN, 1);
   double l_irvi_104 = iRVI(NULL, 0, 16, MODE_MAIN, 1);
   double l_irvi_112 = iRVI(NULL, 0, 24, MODE_MAIN, 1);
   double l_iatr_120 = iATR(NULL, 0, 14, 1);
   double l_imacd_128 = iMACD(NULL, 0, 12, 26, 9, PRICE_TYPICAL, MODE_MAIN, 1);
   if (!gi_140) {
      ls_136 = Symbol();
      li_144 = trade(ls_136,l_irsi_0, l_irsi_8, l_irsi_16, l_icci_24, l_icci_32, l_icci_40, l_iwpr_48, l_iwpr_56, l_iwpr_64, l_istochastic_72, l_istochastic_80, l_istochastic_88, l_irvi_96, l_irvi_104, l_irvi_112, l_iatr_120, l_imacd_128);
   
      if (li_144 == 1 && Stealth_BUY==true) {
         if (lots == 0.0) lots = MMLots(gd_112);
         sendOrder(OP_BUY, lots, 0, 0, Slippage);
         gi_140 = TRUE;
      } else {
         if (li_144 == 2 && Stealth_SELL==true) {
            if (lots == 0.0) lots = MMLots(gd_112);
            sendOrder(OP_SELL, lots, 0, 0, Slippage);
            gi_140 = TRUE;
         }
      }
   }
   OrdersList();
   managePositions();
   return (0);
}

void managePositions() {
   double ld_0 = 0;
   for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
      OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != magicNumber) continue;
      if (OrderType() == OP_BUY) {
         ld_0 = (Bid - OrderOpenPrice()) / Point;
         if (ld_0 >= takeprofit || ld_0 <= (-1 * stoploss) || (ld_0 <= (-1*TrendStopLoss) && OrderLots() < lots)) closeOrder(0);
      } else {
         if (OrderType() == OP_SELL) {
            ld_0 = (OrderOpenPrice() - Ask) / Point;
            if (ld_0 >= takeprofit || ld_0 <= (-1 * stoploss) || (ld_0 <= (-1*TrendStopLoss) && OrderLots() < lots)) closeOrder(1);
         }
      }
   }
}

void OrdersList()
{        
    for (int cnt=0;cnt<OrdersTotal();cnt++)
    { 
     OrderSelect(cnt, SELECT_BY_POS);      
        if (OrderSymbol()==Symbol()&&OrderMagicNumber()==magicNumber) 
        {
            if (OrderType()==OP_BUY)
            {
			         //---- PartialTP1
                  if (Bid-OrderOpenPrice()>MyPoint*PartialTP1&&PartialTP1>0&&OrderLots()*PartialRatio1*PartialRatio2/10000>=0.1&&(lots-0.05)>=OrderLots()) 
                  {
                  OrderClose(OrderTicket(),NormalizeDouble(OrderLots()*PartialRatio1/100,2),Bid,Slippage,Red);
                  continue;
			      }
			         //---- PartialTP2
                  if (Bid-OrderOpenPrice()>MyPoint*PartialTP2&&PartialTP2>0&&NormalizeDouble((lots-0.05)*PartialRatio1/100,2)>=OrderLots()) 
                  {
                  OrderClose(OrderTicket(),NormalizeDouble(OrderLots()*PartialRatio2/100,2),Bid,Slippage,Red);
                  continue;
			      }
			   }
            if (OrderType()==OP_SELL)
            {
                  //---- PartialTP1
                  if (OrderOpenPrice()-Ask>MyPoint*PartialTP1&&PartialTP1>0&&OrderLots()*PartialRatio1*PartialRatio2/10000>=0.1&&(lots-0.05)>=OrderLots()) 
                  {
                  OrderClose(OrderTicket(),NormalizeDouble(OrderLots()*PartialRatio1/100,2),Ask,Slippage,Red);
                  continue;
			      }
			         //---- PartialTP2
                  if (OrderOpenPrice()-Ask>MyPoint*PartialTP2&&PartialTP2>0&&NormalizeDouble((lots-0.05)*PartialRatio1/100,2)>=OrderLots()) 
                  {
                  OrderClose(OrderTicket(),NormalizeDouble(OrderLots()*PartialRatio2/100,2),Ask,Slippage,Red);
                  continue;
			      }
            }
         }   
      } 
}

void closeOrder(int ai_0) {
   double l_price_8;
   for (int l_count_4 = 0; l_count_4 < 10; l_count_4++) {
      if (!IsTradeContextBusy() && IsTradeAllowed()) {
         RefreshRates();
         l_price_8 = nd(Bid);
         if (ai_0 == 1) l_price_8 = nd(Ask);
         if (OrderTicket() == 0) break;
         if (OrderClose(OrderTicket(), OrderLots(), l_price_8, Slippage)) break;
         Print("Order close error: ", ErrorDescription(GetLastError()));
         FileWrite(g_file_128, "Order close error: ", ErrorDescription(GetLastError()));
         FileWrite(g_file_128, OrderTicket(), OrderLots(), l_price_8, Slippage);
         FileFlush(g_file_128);
      }
      Sleep(1000);
   }
}

int sendOrder(int a_cmd_0, double a_lots_4, double a_price_12, double a_price_20, double a_slippage_28 = 0.0) {
   double l_price_40;
   string ls_unused_48;
   int l_ticket_56;
   for (int l_count_36 = 0; l_count_36 < 10; l_count_36++) {
      if (!IsTradeContextBusy() && IsTradeAllowed()) {
         RefreshRates();
         l_price_40 = Ask;
         ls_unused_48 = "LONG";
         if (a_cmd_0 == OP_SELL) {
            l_price_40 = Bid;
            ls_unused_48 = "SHORT";
         }
         l_ticket_56 = OrderSend(Symbol(), a_cmd_0, a_lots_4, l_price_40, a_slippage_28, a_price_12, a_price_20, "BlackCricket", magicNumber, 0, Red);
         if (l_ticket_56 <= 0) {
            Print("Order send error: ", ErrorDescription(GetLastError()));
            FileWrite(g_file_128, "Order send error: ", ErrorDescription(GetLastError()));
         } else return (l_ticket_56);
      }
      Sleep(1000);
   }
   return (l_ticket_56);
}

double MMLots(double ad_0) {
   double ld_8 = AccountLeverage() / 100;
   double ld_16 = NormalizeLots(ad_0 * AccountFreeMargin() / (MarketInfo(Symbol(), MODE_MARGINREQUIRED) * ld_8));
   return (ld_16);
}

double NormalizeLots(double ad_0) {
   double l_lotstep_8 = MarketInfo(Symbol(), MODE_LOTSTEP);
   double l_minlot_16 = MarketInfo(Symbol(), MODE_MINLOT);
   double l_maxlot_24 = MarketInfo(Symbol(), MODE_MAXLOT);
   double ld_32 = MathCeil(ad_0 / l_lotstep_8) * l_lotstep_8;
   ld_32 = MathMin(MathMax(ld_32, l_minlot_16), l_maxlot_24);
   return (ld_32);
}

double nd(double ad_0) {
   return (NormalizeDouble(ad_0, Digits));
}

bool isNewBar() {
   if (g_bars_124 < Bars) {
      g_bars_124 = Bars;
      return (TRUE);
   }
   return (FALSE);
}

int trade(string ls_136, double l_irsi_0,double l_irsi_8,double l_irsi_16,double l_icci_24,double l_icci_32,double l_icci_40,double l_iwpr_48,double l_iwpr_56,double l_iwpr_64,double l_istochastic_72,double l_istochastic_80,double l_istochastic_88,double l_irvi_96,double l_irvi_104,double l_irvi_112,double l_iatr_120,double l_imacd_128 )
{
   if(ls_136 == "EURUSD")
      return(eurusd(l_irsi_0, l_irsi_8, l_irsi_16, l_icci_24, l_icci_32, l_icci_40, l_iwpr_48, l_iwpr_56, l_iwpr_64, l_istochastic_72, l_istochastic_80, l_istochastic_88, l_irvi_96, l_irvi_104, l_irvi_112, l_iatr_120, l_imacd_128));
   if(ls_136 == "GBPUSD")
      return(gbpusd(l_irsi_0, l_irsi_8, l_irsi_16, l_icci_24, l_icci_32, l_icci_40, l_iwpr_48, l_iwpr_56, l_iwpr_64, l_istochastic_72, l_istochastic_80, l_istochastic_88, l_irvi_96, l_irvi_104, l_irvi_112, l_iatr_120, l_imacd_128));
   if(ls_136 == "EURCHF")
      return(eurchf(l_irsi_0, l_irsi_8, l_irsi_16, l_icci_24, l_icci_32, l_icci_40, l_iwpr_48, l_iwpr_56, l_iwpr_64, l_istochastic_72, l_istochastic_80, l_istochastic_88, l_irvi_96, l_irvi_104, l_irvi_112, l_iatr_120, l_imacd_128));
   if(ls_136 == "EURGBP")
      return(eurgbp(l_irsi_0, l_irsi_8, l_irsi_16, l_icci_24, l_icci_32, l_icci_40, l_iwpr_48, l_iwpr_56, l_iwpr_64, l_istochastic_72, l_istochastic_80, l_istochastic_88, l_irvi_96, l_irvi_104, l_irvi_112, l_iatr_120, l_imacd_128));
   if(ls_136 == "USDJPY")
      return(usdjpy(l_irsi_0, l_irsi_8, l_irsi_16, l_icci_24, l_icci_32, l_icci_40, l_iwpr_48, l_iwpr_56, l_iwpr_64, l_istochastic_72, l_istochastic_80, l_istochastic_88, l_irvi_96, l_irvi_104, l_irvi_112, l_iatr_120, l_imacd_128));
   if(ls_136 == "USDCAD")
      return(usdcad(l_irsi_0, l_irsi_8, l_irsi_16, l_icci_24, l_icci_32, l_icci_40, l_iwpr_48, l_iwpr_56, l_iwpr_64, l_istochastic_72, l_istochastic_80, l_istochastic_88, l_irvi_96, l_irvi_104, l_irvi_112, l_iatr_120, l_imacd_128));
   if(ls_136 == "AUDUSD")
      return(audusd(l_irsi_0, l_irsi_8, l_irsi_16, l_icci_24, l_icci_32, l_icci_40, l_iwpr_48, l_iwpr_56, l_iwpr_64, l_istochastic_72, l_istochastic_80, l_istochastic_88, l_irvi_96, l_irvi_104, l_irvi_112, l_iatr_120, l_imacd_128));
   if(ls_136 == "EURJPY")
      return(eurjpy(l_irsi_0, l_irsi_8, l_irsi_16, l_icci_24, l_icci_32, l_icci_40, l_iwpr_48, l_iwpr_56, l_iwpr_64, l_istochastic_72, l_istochastic_80, l_istochastic_88, l_irvi_96, l_irvi_104, l_irvi_112, l_iatr_120, l_imacd_128));
   if(ls_136 == "GBPJPY")
      return(gbpjpy(l_irsi_0, l_irsi_8, l_irsi_16, l_icci_24, l_icci_32, l_icci_40, l_iwpr_48, l_iwpr_56, l_iwpr_64, l_istochastic_72, l_istochastic_80, l_istochastic_88, l_irvi_96, l_irvi_104, l_irvi_112, l_iatr_120, l_imacd_128));
   if(ls_136 == "USDCHF")
      return(usdchf(l_irsi_0, l_irsi_8, l_irsi_16, l_icci_24, l_icci_32, l_icci_40, l_iwpr_48, l_iwpr_56, l_iwpr_64, l_istochastic_72, l_istochastic_80, l_istochastic_88, l_irvi_96, l_irvi_104, l_irvi_112, l_iatr_120, l_imacd_128));
}






